#include <string.h>

#include "check.h"
#include "security.h"
#include "security_error.h"

static char decode[] = "test";
static char encode[] = "dGVzdA==";

static int decode_len = 4;
static int encode_len = 8;

START_TEST(test_ok) {
    char ret_decode[256] = { 0 };
    char ret_encode[256] = { 0 };
    size_t len_de = 0, len_en = 0;
    ck_assert_int_eq(security_base64_encode(ret_encode, &len_en, sizeof(ret_encode), decode, decode_len), SECURITY_OK);
    ck_assert_int_eq(security_base64_decode(ret_decode, &len_de, sizeof(ret_decode), encode, encode_len), SECURITY_OK);
    ck_assert_str_eq(ret_encode, encode);
    ck_assert_str_eq(ret_decode, decode);
}
END_TEST

START_TEST(test_input) {
    char buffer[256] = { 0 };
    size_t len = 0;

    ck_assert_int_eq(security_base64_encode(NULL, &len, sizeof(buffer), decode, decode_len), SECURITY_ERR_POINTER_NULL);
    ck_assert_int_eq(security_base64_encode(buffer, NULL, sizeof(buffer), decode, decode_len), SECURITY_ERR_POINTER_NULL);
    ck_assert_int_eq(security_base64_encode(buffer, &len, 0, decode, decode_len), SECURITY_ERR_BAD_INPUT);
    ck_assert_int_eq(security_base64_encode(buffer, &len, sizeof(buffer), NULL, decode_len), SECURITY_ERR_POINTER_NULL);
    ck_assert_int_eq(security_base64_encode(buffer, &len, sizeof(buffer), decode, 0), SECURITY_ERR_BAD_INPUT);

    ck_assert_int_eq(security_base64_decode(NULL, &len, sizeof(buffer), decode, decode_len), SECURITY_ERR_POINTER_NULL);
    ck_assert_int_eq(security_base64_decode(buffer, NULL, sizeof(buffer), decode, decode_len), SECURITY_ERR_POINTER_NULL);
    ck_assert_int_eq(security_base64_decode(buffer, &len, 0, decode, decode_len), SECURITY_ERR_BAD_INPUT);
    ck_assert_int_eq(security_base64_decode(buffer, &len, sizeof(buffer), NULL, decode_len), SECURITY_ERR_POINTER_NULL);
    ck_assert_int_eq(security_base64_decode(buffer, &len, sizeof(buffer), decode, 0), SECURITY_ERR_BAD_INPUT);

    ck_assert_int_eq(security_base64_decode(buffer, &len, sizeof(buffer), "????", 4), SECURITY_BASE64_INVALID_CHARACTER);
}
END_TEST

Suite *check_base64(void)
{
    Suite *s;
    TCase *tc_core;

    s = suite_create("base64 test");

    tc_core = tcase_create("core");
    tcase_add_test(tc_core, test_ok);
    tcase_add_test(tc_core, test_input);

    suite_add_tcase(s, tc_core);

    return s;
}